$(document).ready(function () {
    slide();
    magnifier();

    var params = Params();
    var product = LsyStorage.getItem('proList_' + params.id);
    $("#name").text(product.name);
    $("#price").text(product.price);

    $("#add-cart").click(function () {
        var num = $("#quantity").val();
        var item = LsyStorage.getItem('proList_' + params.id);
        item.num = num;
        LsyStorage.setItem('shopCar_' + params.id, item);
        alert('商品已加入购物车');
    });

    function Params() {
        var result = {}, hash = location.hash, search = location.search;
        result.docName = decodeURIComponent(location.pathname).match(/\/([^\/]+)\.html$/)[1];//提取文件名
        if (hash) {
            result.hash = hash.replace(/^\#/, ''); //提前锚点
        }
        if (search) {
            var arr = search.replace(/^\?/, '').split(/\&/g); //提前所有search参数
            for (var i = 0, n = arr.length; i < n; i++) {
                var key = arr[i].split(/\=/)[0];
                result[key] = decodeURIComponent(arr[i].split(/\=/)[1]);
            }
        }
        return result;
    }

    $(".buy-btn").click(function () {
        window.location.href = "confirm_order.html";
    });

    $(".control .plus").click(function () {
        var _num = $('#quantity');
        var num = _num.val();
        num++;
        _num.val(num);
    });

    $(".control .less").click(function () {
        var _num = $('#quantity');
        var num = _num.val();
        if (num > 1) {
            num--;
        }
        _num.val(num)
    });

    $('#quantity').keyup(function () {
        var _vm = $(this),
            val = _vm.val();
        val = val.replace(/[^\d]/g, '').replace(/^0+/, '');
        if(val==''){
            _vm.val(1);
        }else {
            _vm.val(val);
        }
    });

    function magnifier() {
        var num = 0;
        var index = 0;
        $(".buy-left ul ol li").mouseenter(function () {
            index = $(this).index();
            $(this).children('img').addClass('on').parents('li').siblings().children('img').removeClass('on');
            var src = $(this).children('img').attr('src');
            $(".big img").attr('src', src);
            $(".super-big img").attr('src', src);
        });
        $("#first").click(function () {
            num--;
            if (num < 0) {
                num = 1;
                $(".center ol").css('left', -614);
            }
            $('.center ol').stop().animate({'left': -num * 307}, 300);
        });
        $("#last").click(function () {
            num++;
            if (num > 2) {
                num = 1;
                $('.center ol').css('left', 0);
            }
            $('.center ol').stop().animate({'left': -num * 307}, 300);
        });

        $(".big").hover(function () {
            $(".magnifier,.super-big").show();
        }, function () {
            $(".magnifier,.super-big").hide();
        });
        $(".big").mousemove(function (event) {
            var e = event || window.event;
            var x = e.pageX - $(this).offset().left - 84.75;
            var y = e.pageY - $(this).offset().top - 92.75;
            if (x < 0) {
                x = 0;
            }
            if (x > 169.5) {
                x = 169.5;
            }
            if (y < 0) {
                y = 0;
            }
            if (y > 185.5) {
                y = 185.5
            }
            $(".magnifier").css({
                'left': x,
                'top': y
            });
            $(".super-big img").css({
                'left': -x * 2,
                'top': -y * 2
            })
        });
    }

    function slide() {
        var num = 0;
        $(".left").click(function (event) {
            num--;
            if (num < 0) {
                num = 1;
                $(".surround ul").css('left', -2254);
            }
            $('.surround ul').stop().animate({'left': -num * 1127}, 1000);
        });
        $(".right").click(function (event) {
            num++;
            if (num > 2) {
                num = 1;
                $('.surround ul').css('left', 0);
            }
            $('.surround ul').stop().animate({'left': -num * 1127}, 1000);
        });
        $(".sort").hover(function () {
            $(".nav-list").stop().slideDown();
        }, function () {
            $(".nav-list").stop().slideUp();
        });
        $(".menu li").mouseover(function () {
            $(this).addClass('on').siblings().removeClass('on');
        });
    }

});